package com.a01_2021.thousandstimes.instance.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.instance.service.IInterpolateAttachmentService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/interpolateAttachment")
public class InterpolateAttachmentController {

    @Resource
    IInterpolateAttachmentService interpolateAttachmentService;

    /**
     * 上传内推附件
     *
     * @param instanceId 实例id
     * @param file       文件
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('interpolateAttachment-upload')")
    @DS("#header.instanceid")
    @PostMapping("upload")
    public AjaxResponse update(@RequestHeader("instanceid") String instanceId, MultipartFile file) throws IOException, CustomException {
        if (instanceId.equals("master")) {
            return AjaxResponse.error(400, "实例接口不允许SAAS账户访问");
        }
        String url = interpolateAttachmentService.update(file, instanceId);
        return AjaxResponse.success(url);
    }
}
